//会员列表
app.controller('UserList', function($scope, $http, $httpParamSerializer, $location, common){
    //变量声明
    $scope.list     = new Array();
    $scope.nItem    = new Object();
    $scope.nRela    = new Object();
    $scope.loading  = false;
    $scope.pageHtml     = '';
    $scope.nPage    = 1;
    $scope.count    = 0;
    $scope.condition    = {
        keyword     : '',
        date_start  : '',
        date_end    : '',
        father_id   : ''
    }
    $scope.breadcrumb   = [{
        'father_id' : '',
        'nickname'  : '全部用户'
    }]
    $scope.sortInfo     = {
        'field' : 'id',
        'desc'  : 1
    }

    //初始化JS
    $scope.init_js  = function(){
        $('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 2
        });

        //回车键监听
        common.listenEnterKey(function(){
            $scope.get_list(1);
        });
    }

    //加载数据
    $scope.get_list     = function(page, callback){
        var url     = config.prefix_url + '/User/getList?p='+page;
        var param   = {
            condition   : $scope.condition,
            sort_info   : $scope.sortInfo
        }

        $http.post(url, param).success(function(info){
            $scope.loading  = false;
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.list     = info.data.list;
            $scope.nPage    = page;
            $scope.pageHtml     = info.data.page_html;
            $scope.count    = info.data.count;
            common.startPage($scope.get_list);

            //回调执行
            if(callback)    callback();
        })

        $scope.loading  = true;
    }

    //详情获取
    $scope.get_item     = function(id, modal){
        let url     = config.prefix_url + '/User/getItem?id='+id;
        $http.get(url).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.nItem    = info.data.user_info;
            $scope.nRela    = info.data.rela_info;

            $(modal).modal('show');
        })
        $scope.loading  = true;
    }

    //保存数据
    $scope.update_item  = function(){
        let url     = config.prefix_url + '/User/updateItem';
        let param   = {
            'id'    : $scope.nItem.id,
            'info'  : $scope.nItem
        }

        $http.post(url, param).success(function(info){
            console.log(info);
            $scope.loading  = false;
            $('.modal.edit-item').modal('hide');
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
        })

        $scope.loading  = true;
    }

    //获取下级用户列表
    $scope.get_child_list   = function(fatherId, nickname){
        //面包屑导航处理
        var breadcrumb  = new Array();
        for(var k in $scope.breadcrumb){
            if($scope.breadcrumb[k].father_id == fatherId)
                break;
            breadcrumb.push($scope.breadcrumb[k]);
        }
        breadcrumb.push({'father_id' : fatherId, 'nickname' : nickname});

        $scope.condition.father_id     = fatherId;
        $scope.condition.keyword  = '';
        $scope.get_list(1, function(){
            $scope.breadcrumb   = breadcrumb;
        });
    }

    //更改排序信息
    $scope.change_sort  = function(field){
        //排序信息处理
        let dom     = $('[data-sort="'+field+'"]');
        if(dom.hasClass('fa-sort-desc')){
            $scope.sortInfo.desc    = 0;
        }else{
            $scope.sortInfo.desc    = 1;
        }
        $scope.sortInfo.field   = field;

        //加载数据
        $scope.get_list(1, function(){
            //还原排序图标
            $('.sort').children('.fa')
                .removeClass('fa-sort-desc')
                .removeClass('fa-sort-asc')
                .addClass('fa-sort');
            //排序图标添加
            if($scope.sortInfo.desc){
                $(dom).removeClass('fa-sort').addClass('fa-sort-desc');
            }else{
                $(dom).removeClass('fa-sort').addClass('fa-sort-asc');
            }
        });
    }

    //导出数据
    $scope.out_excel    = function(){
        let url     = config.prefix_url + '/User/outExcel?';
        let param   = {
            'condition'   : $scope.condition,
            'sort_info' : $scope.sortInfo
        }
        $http.get(url + '?' + httpBuildQuery(param)).success(function(info){
            console.log(info);
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            let count   = parseInt(info.data.count);
            param.start     = 0;
            param.length    = 5000;
            while(param.start < count){
                window.open(url + '?' + httpBuildQuery(param), '_blank');
                param.start     = param.start + param.length;
            }
        })

        $scope.loading  = true;
    }

    $scope.init_js();
    $scope.get_list(1);
});

//vip列表
app.controller('UserVipList', function($scope, $http, $location, common){
    //变量声明
    $scope.list     = new Array();
    $scope.loading  = false;
    $scope.nItem    = new Object();
    $scope.nPage    = 1;
    $scope.pageHtml = '';
    $scope.count    = 0;
    $scope.condition    = {
        keyword     : '',
        date_start  : '',
        date_end    : ''
    }
    $scope.sortInfo     = {
        field   : 'id',
        desc    : 1
    }

    $scope.init_js  = function(){
        $('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 2
        });

        //回车键监听
        common.listenEnterKey(function(){
            $scope.get_list(1);
        });
    }

    //加载列表
    $scope.get_list     = function(page, callback){
        var page    = page ? page : 1;
        var url     = config.prefix_url + '/User/getVipList?p='+page;
        var param   = {
            condition   : $scope.condition,
            sort_info   : $scope.sortInfo
        }

        $http.post(url, param).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.list     = info.data.list;
            $scope.nPage    = page;
            $scope.pageHtml     = info.data.page_html;
            $scope.count    = info.data.count;

            common.startPage($scope.get_list);

            if(callback)    callback();
        })

        $scope.loading  = true;
    }

    //条目详情获取
    $scope.get_item     = function(item){
        console.log(item);
        $scope.nItem    = item;
        $('.modal.edit-item').modal('show');
    }

    //更改信息
    $scope.update_item  = function(userId, userInfo){
        var url     = config.prefix_url + '/User/updateItem';
        var param   = {
            'id'   : userId,
            'info' : {
                'child_name'    : userInfo.child_name,
                'child_birthday_date'   : userInfo.child_birthday_date,
                'child_city'    : userInfo.child_city,
                'child_school'  : userInfo.child_school,
                'child_course_name' : userInfo.child_course_name,
                'child_sex'     : userInfo.child_sex,
                'child_level'   : userInfo.child_level
            }
        }

        $http.post(url, param).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $('.modal.edit-item').modal('hide');
            $scope.get_list($scope.nPage);
        })

        $scope.loading  = true;
    }

    //排序
    $scope.change_sort  = function(field){
        let dom     = $('[data-sort="'+field+'"]');
        if($(dom).hasClass('fa-sort-desc')){
            $scope.sortInfo.desc    = 0;
        }else{
            $scope.sortInfo.desc    = 1;
        }
        $scope.sortInfo.field   = field;
        
        $scope.get_list(1, function(){
            //还原排序图标
            $('.sort').children('.fa')
                .removeClass('fa-sort-desc')
                .removeClass('fa-sort-asc')
                .addClass('fa-sort');
            //排序图标添加
            if($scope.sortInfo.desc){
                $(dom).removeClass('fa-sort').addClass('fa-sort-desc');
            }else{
                $(dom).removeClass('fa-sort').addClass('fa-sort-asc');
            }
        });  
    }

    //导出数据
    $scope.out_excel    = function(){
        let url     = config.prefix_url + '/User/outVipExcel?';
        let param   = {
            'condition'   : $scope.condition,
            'sort_info' : $scope.sortInfo
        }
        $http.get(url + '?' + httpBuildQuery(param)).success(function(info){
            console.log(info);
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            let count   = parseInt(info.data.count);
            param.start     = 0;
            param.length    = 5000;
            while(param.start < count){
                window.open(url + '?' + httpBuildQuery(param), '_blank');
                param.start     = param.start + param.length;
            }
        })

        $scope.loading  = true;
    }

    $scope.get_list(1);
    $scope.init_js();
})